//
//  DiaoDuEditVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/8/19.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "DiaoDuEditVC.h"
#import "DiaoDuMainModel.h"
#import "SupplierMainModel.h"
#import "DiaoDuEditCell.h"
#import "SetDiaoDuVC.h"
#import "CarMainModel.h"

@interface DiaoDuEditVC ()<UITableViewDelegate,UITableViewDataSource> {
    BOOL isAllSelected;
    int page;
}

@property (nonatomic, strong) NSMutableArray <CarMainModel *>*array;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIButton *allBtn;

@end

@implementation DiaoDuEditVC

-(NSMutableArray<CarMainModel *> *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 70, ScreenW, 70 + BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        
        self.allBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 23, 55, 24)];
        [self.allBtn setTitle:@"全选" forState:0];
        [self.allBtn setTitleColor:HexColor(0x2B2C44) forState:0];
        self.allBtn.titleLabel.font = BoldFontOfSize(14);
        [self.allBtn addTarget:self action:@selector(allClick) forControlEvents:UIControlEventTouchUpInside];
        [self.allBtn setImage:[UIImage imageNamed:@"icon_backBox_choice"] forState:0];
        self.allBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -4, 0, 0);
        [_bottomView addSubview:self.allBtn];
        
        UIButton *stopBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenW - 15 - 68, 20, 68, 30)];
        [stopBtn setBackgroundColor:Color_ThemeColor];
        stopBtn.layer.masksToBounds = YES;
        stopBtn.layer.cornerRadius = 15;
        [stopBtn setTitle:@"选择3PL" forState:0];
        [stopBtn setTitleColor:HexColor(0xffffff) forState:0];
        stopBtn.titleLabel.font = BoldFontOfSize(14);
        [stopBtn addTarget:self action:@selector(stopClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:stopBtn];
        
        UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(stopBtn.nim_left - 20 - 96, 20, 96, 30)];
        [cancleBtn setBackgroundColor:HexColor(0xF7F7FA)];
        cancleBtn.layer.masksToBounds = YES;
        cancleBtn.layer.cornerRadius = 15;
        [cancleBtn setTitle:@"取消操作" forState:0];
        [cancleBtn setTitleColor:HexColor(0x585974) forState:0];
        cancleBtn.titleLabel.font = BoldFontOfSize(14);
        [cancleBtn addTarget:self action:@selector(cancleClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:cancleBtn];
    }
    return _bottomView;
}

- (void)stopClick {
    NSMutableString *str = [NSMutableString stringWithString:@""];
    __block BOOL canGo = NO;
    [self.array enumerateObjectsUsingBlock:^(CarMainModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.isSelected) {
            canGo = YES;
            [str appendFormat:@"%@,",obj.vehicle_id];
        }
    }];
    if (str.length) {
        [str deleteCharactersInRange:NSMakeRange(str.length - 1, 1)];
    }
    if (canGo) {
        SetDiaoDuVC *vc = [SetDiaoDuVC new];
        vc.vehicle_ids = str;
        [self.rt_navigationController pushViewController:vc animated:YES];
    }else {
        [MBProgressHUD leShowFailedHUD:MAIN_WINDOW message:@"请选择车辆"];
    }
}

- (void)cancleClick {
    [self.rt_navigationController popViewControllerAnimated:YES];
}

- (void)allClick {
    if (isAllSelected) {
        isAllSelected = NO;
        [self.allBtn setImage:[UIImage imageNamed:@"icon_backBox_choice"] forState:0];
        [self.array enumerateObjectsUsingBlock:^(CarMainModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.isSelected = NO;
        }];
        [self.tableView reloadData];
    }else {
        isAllSelected = YES;
        [self.allBtn setImage:[UIImage imageNamed:@"icon_backBox_choice_sel"] forState:0];
        [self.array enumerateObjectsUsingBlock:^(CarMainModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.isSelected = YES;
        }];
        [self.tableView reloadData];
    }
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - 64 - BottomHeight)];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            __strong_self;
//            strongSelf->page = 1;
//            [strongSelf requestForList];
//        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"供应商调度";
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    page = 1;
    [self requestForList];
}

- (void)requestForList {
    [HttpRequest PostWithApi:LogisticsSupplierCarList params:@{@"page":@(page)} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.array removeAllObjects];
                self.array = [CarMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            }else {
                NSArray *array = [CarMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.array addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
//            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
    } fail:^(NSError * _Nonnull error) {
//        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DiaoDuEditCell *cell = [DiaoDuEditCell DiaoDuEditCellWithTableView:tableView];
    CarMainModel *model = self.array[indexPath.row];
    cell.model = model;
    cell.chooseBlock = ^{
        model.isSelected = !model.isSelected;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    DiaoDuEditCell *cell = [DiaoDuEditCell DiaoDuEditCellWithTableView:tableView];
    cell.model = self.array[indexPath.row];
    return cell.height;
}

@end
